package com.fancy.learncenter.ui.adapter;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.fancy.learncenter.R;
import com.fancy.learncenter.common.VideoSentenceDataBean;

import java.util.List;

/**
 * Created by hyy on 2018/4/18.
 */

public class CartoonSentenceLvAdapter extends BaseAdapter {
    public CartoonSentenceLvAdapter() {
    }

    Context mContext;
    List<VideoSentenceDataBean> itemDatas;

    public CartoonSentenceLvAdapter(Context context, List<VideoSentenceDataBean> itemDatas) {
        this.mContext = context;
        this.itemDatas = itemDatas;

        this.inflate = LayoutInflater.from(mContext);
    }

    //   0 默认   1 本地  2预览 3线上
    private String type = "";

    public CartoonSentenceLvAdapter(Context context, List<VideoSentenceDataBean> itemDatas, String type) {
        this.mContext = context;
        this.itemDatas = itemDatas;
        this.type = type;
        this.inflate = LayoutInflater.from(mContext);
    }

    LayoutInflater inflate;


    @Override
    public int getCount() {
        return itemDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return itemDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup viewGroup) {

        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflate.inflate(R.layout.cartoon_sentence_item, null);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.record_icon = convertView.findViewById(R.id.record_icon);
        holder.video_voice_icon = convertView.findViewById(R.id.video_voice_icon);
        holder.play_record_icon = convertView.findViewById(R.id.play_record_icon);

        holder.content = convertView.findViewById(R.id.content);

        holder.record_pb = convertView.findViewById(R.id.record_pb);

        holder.current_index_text = convertView.findViewById(R.id.current_index_text);
        holder.status_tv = convertView.findViewById(R.id.status_tv);

        holder.content.setText(itemDatas.get(position).getTranslation());

        holder.current_index_text.setText((position + 1) + "/" + itemDatas.size());


        holder.record_pb.setVisibility(View.GONE);


        if (type.equals("1")) {
            holder.record_icon.setVisibility(View.GONE);
        } else if (type.equals("2")) {
            holder.record_icon.setVisibility(View.GONE);
            holder.play_record_icon.setVisibility(View.GONE);
        }


        if (isSelected == position) {
            holder.record_icon.setImageURI("res://com.fancy.learncenter/" + R.mipmap.cartoon_sentence_record_icon);

            holder.video_voice_icon.setImageURI("res://com.fancy.learncenter/" + R.mipmap.cartoon_sentence_voice_icon);

            if (itemDatas.get(position).getStatus() == 0) {
                holder.status_tv.setVisibility(View.GONE);
                holder.play_record_icon.setImageURI(Uri.parse("res://com.fancy.learncenter/" + R.mipmap.cartoon_sentence_pause_icon_recorded));
            } else {
                holder.status_tv.setVisibility(View.VISIBLE);
                holder.play_record_icon.setImageURI(Uri.parse("res://com.fancy.learncenter/" + R.mipmap.cartoon_sentence_pause_cion));
            }

        } else {
            holder.record_icon.setImageURI("res://com.fancy.learncenter/" + R.mipmap.cartoon_sentence_record_icon_gray);

            holder.video_voice_icon.setImageURI("res://com.fancy.learncenter/" + R.mipmap.cartoon_sentence_voice_icon_gray);

            holder.play_record_icon.setImageURI("res://com.fancy.learncenter/" + R.mipmap.cartoon_sentence_pause_gray_icon);
        }


        holder.record_icon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (isSelected == position) {
                    onClickCallBack.recordClick();
                }

            }
        });

        holder.video_voice_icon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isSelected == position) {
                    onClickCallBack.videoClick();
                }

            }
        });

        holder.play_record_icon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isSelected == position) {
                    onClickCallBack.playRecordClick();
                }

            }
        });

        return convertView;
    }

    public class ViewHolder {
        public SimpleDraweeView record_icon;
        public SimpleDraweeView video_voice_icon;
        public SimpleDraweeView play_record_icon;

        public TextView content;
        public TextView current_index_text;
        public TextView status_tv;

        public ProgressBar record_pb;
    }

    int isSelected = 0;

    public void setSelectItem(int isSelect, boolean isSelected) {
        this.isSelected = isSelect;
        notifyDataSetChanged();
    }

    private OnClickCallBack onClickCallBack;

    public OnClickCallBack getOnClickCallBack() {
        return onClickCallBack;
    }

    public void setOnClickCallBack(OnClickCallBack onClickCallBack) {
        this.onClickCallBack = onClickCallBack;
    }

    public interface OnClickCallBack {
        abstract void recordClick();

        abstract void videoClick();

        abstract void playRecordClick();
    }
}
